package com.bing.services.impl;

import com.bing.Component.Context;
import com.bing.bean.weixin.ResponseMessage;
import com.bing.utils.MsgHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/6/7.
 */
public class EventCommandServiceImpl extends AbstractCommandService {
    private static final Logger logger= LoggerFactory.getLogger(EventCommandServiceImpl.class);
    protected int order = 2;
    private static final String TWEVENT = TW + "event";


    private static List<String> contexts = new ArrayList<String>() {
        {
            add(TWEVENT);
        }
    };

    @Override
    protected List<String> getContexts() {
        return contexts;
    }

    @Override
    protected Logger getLogger() {
        return logger;
    }

    @Override
    public ResponseMessage handle(Context context) {
        switch (context.getRequestMessage().getEvent()) {
            case CLICK:
                switch (context.getRequestMessage().getEventKey()) {
                    case "help":
                        return MsgHelper.toText(context.getRequestMessage(), "您好!有什么可以帮到您?");

                    default:
                        return MsgHelper.toText(context.getRequestMessage(), context.getRequestMessage().getEventKey());
                }

            case subscribe:
                return MsgHelper.toText(context.getRequestMessage(), "您好!谢谢关注!");

            case unsubscribe:

                break;
            default:

        }
        return MsgHelper.toText(context.getRequestMessage(), "");
    }

    @Override
    public boolean canHandle(Context context) {
        if (!StringUtils.hasText(context.getCommand())) {
            return false;
        }
        for (String s : getContexts()) {
            if (s.equalsIgnoreCase(context.getCommand())) {
                getLogger().info("适配命令:{}, 事件类型:{}, 文本内容:{}", s, context.getRequestMessage().getEvent(), context.getRequestMessage().getContent());
                return true;
            }
        }
        return false;
    }
}
